/*
* Copyright (C) 2010-2012 Magnus Skjegstad
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
* 
* The latest version of this library can be downloaded from http://mist-pubsub.googlecode.com
* 
*/
package com.skjegstad.mist2.demos;

import com.skjegstad.mist2.IMistSubscriptionListener;
import com.skjegstad.mist2.MistCoreManager;
import com.skjegstad.mist2.MistUtil;
import com.skjegstad.mist2.datatypes.DataElement;
import com.skjegstad.mist2.exceptions.MistException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 *
 * @author Magnus Skjegstad
 */
public class TestShortStringConsumer {
    public static void main(String[] argv) throws UnknownHostException, InterruptedException, MistException, SocketException {
        MistCoreManager mist = new MistCoreManager();
        //mist.enableBroadcast(InetAddress.getByName("10.0.1.255"), 1234);
        //mist.enableMulticast(InetAddress.getByName("224.0.0.66"), 1234, MistUtil.askUserForNetworkInterface());
        
        Properties config = new Properties();
        config.setProperty("fragmentSize", "1000");
        config.setProperty("networkInterfaceName", MistUtil.askUserForNetworkInterface().getName());
        mist.addUDPBroadcastModule(InetAddress.getByName("224.0.0.66"),
                1234, true,
                config, null);
                
        mist.setDebug(true);
        mist.start();

        final List<String> strings = new ArrayList();

        mist.subscribe("1.2.3", new IMistSubscriptionListener() {
            int c = 0;
            public void deliverMessage(DataElement message) {
                synchronized(strings) {
                    strings.add(new String(message.getPayload()));
                    c++;
                    System.err.print(c +",");
                }
            }
        });

        System.out.println("Waiting for messages...");

        while (true)
            Thread.sleep(1000);

    }
}
